<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey,
		getEditorTitle,
		hasEditor
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import moment from '@/static/js/moment.js'
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				resultUserIdCacheKey: 'rps-hggfpj-tsupplierEvaluation-resultUserId-cache',
				supplierIdCacheKey: 'rps-hggfpj-tsupplierEvaluation-supplierId-cache',
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','salaryInstId','salaryInstName'])
		},
		methods:{
			onSubChange(editor){
				let _that = this.$refs.autoform
				let that = this 
				if(editor.groupKey == 'title4' && editor.workList.length) {
					let maxScore = 0;
					let score = 0;
					for(let i=0; i<editor.workList.length; i++){
						if(editor.workList[i].verifyResultCode != '39078'){
							if(editor.workList[i].maxScore){
								maxScore = parseFloat(maxScore) + parseFloat(editor.workList[i].maxScore)
							}
							if(editor.workList[i].score){
								score = parseFloat(score) + parseFloat(editor.workList[i].score)
							}
						}
					}
					_that.formdata.standardScoreSum = maxScore.toFixed(1)
					_that.formdata.scoreSum = score.toFixed(1)
					if (maxScore > 0) {
					    _that.formdata.score = (score / maxScore * 100).toFixed(1)
					} else {
					    _that.formdata.score = 0
					}
				}
			},
			formSubmit(){
				let _that = this
				if(_that.subEditorObj["title4"]){
					let dataList = JSON.parse(_that.formdata.dataStr)
					 for(let index in dataList){
						 if(!dataList[index].fileList){
							 delete dataList[index].fileList
						 }
					 }
					 _that.formdata.dataStr = JSON.stringify(dataList)
				}
				_that.om.formSave("tsupplierEvaluation/addTsupplierEvaluation",_that.formdata,_that)
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'283',
							categoryId:'6431'
						}
					},{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'289',
							categoryId:'9228'
						}
					},{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'289',
							categoryId:'7414'
						}
					}
				]
					if(fileId){
						ajaxArr.push({
							url:"commonForm/getInfo",
							param:{
								fileId:fileId,
								formCode: options.code,
							}
						})
					}

				formModelLoad(ajaxArr,function(res,mainData){
					_that.$refs.autoform.formdata.effectPeriodNum = ''
					changeEditor("title_basic","resultInstId","data",configSelectData([],'id','instName'),mainData)
					changeEditor("title5","isSupplierCode","data",configSelectData(res[0],"code","text"),mainData)
					changeEditor("title5","effectPeriodId","data",configSelectData(res[1],"id","text"),mainData)
					changeEditor("title5","supplierCateId","data",configSelectData(res[2],"id","text"),mainData)
					
					changeEditor("title_basic","resultUserId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({}),
							url:'temployee/getStaffCombogrid',
							namefield:'userPosInstName',
							initload:true,
							searchfield:'q',
							cachename:_that.resultUserIdCacheKey
						}
					},null,mainData)
					
					changeEditor("title1","supplierId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify({
								oldValue: ''
							}),
							url:'tsupplier/likeSerchAllSupplier',
							namefield:'supplierCode',
							notefield: "supplierName",
							initload:true,
							searchfield:'q',
							cachename:_that.supplierIdCacheKey
						}
					},null,mainData)
					
					if (_that.subEditorObj['title4']) {
						let configData = {}
						if(_that.subEditorObj["title4"]){						
							changeEditor('title4', 'dataStr', {
								showDel:false,
								showAdd:false,
								getWorkParam:'getWorkParam',
								titleTemplate: '评价项目：<%= itemName || "无" %>',
							}, null, mainData);
						}
					}
					
					if (_that.subEditorObj['title6']) {
						let configData = {}
						if(_that.subEditorObj["title6"]){						
							changeEditor('title6', 'userStr', {
								titleTemplate: '评审人：<%= userName || "无" %>',
							}, null, mainData);
						}
					}
					
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[3].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[3].value[index])
								_that.$set(_that.formdata,index,res[3].value[index])
							}
						}
						
						_that.$refs.autoform.formdata.effectPeriodNum = res[3].value.effectPeriodNum
						
						changeEditor("title_basic","resultUserId",{
							link:'../../public/pageSearch/pageSearch',
							"linkParam":{
								other:JSON.stringify({
									firstUserId: res[3].value.resultUserId
								}),
								url:'temployee/getStaffCombogrid',
								namefield:'userPosInstName',
								initload:true,
								searchfield:'q',
								cachename:_that.resultUserIdCacheKey
							}
						},null,mainData)
						
						_that.om.request(
							'orgInstPosUserOfficial/findInstByUserIdOrPostId',
							{
								userId: res[3].value.resultUserId,
							}
						).then(res=>{
							_that.$refs.autoform.changeEditor("title_basic","resultInstId","data",configSelectData(res,"id","instName"))
							_that.$refs.autoform.initSelectIndex("title_basic","resultInstId")
						})
						
						changeEditor("title1","supplierId",{
							link:'../../public/pageSearch/pageSearch',
							"linkParam":{
								other:JSON.stringify({
									oldValue: res[3].value.supplierId
								}),
								url:'tsupplier/likeSerchAllSupplier',
								namefield:'supplierCode',
								notefield: "supplierName",
								initload:true,
								searchfield:'q',
								cachename:_that.supplierIdCacheKey
							}
						},null,mainData)
						
						if(_that.subEditorObj["title4"]){
							let datalist = res[3].value.datas
							_that.subEditorObj["title4"].workList = datalist
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(datalist))
							_that.$set(_that.formdata,"dataStr",JSON.stringify(datalist))
						}
						
						if(_that.subEditorObj["title6"]){
							let datalist = res[3].value.tsupplierEvaluation_users
							_that.subEditorObj["title6"].workList = datalist
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"userStr",JSON.stringify(datalist))
							_that.$set(_that.formdata,"userStr",JSON.stringify(datalist))
						}
						
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("title5","isSupplierCode")
							_that.$refs.autoform.initSelectIndex("title5","effectPeriodId")
							_that.$refs.autoform.initSelectIndex("title5","supplierCateId")
						})
					}
					
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.evaluateDate){
							_that.$refs.autoform.formdata.evaluateDate = moment().format('YYYY-MM-DD')
						}
					})
				},options)
			},
			clearResultUserId(){
				let _that = this
				_that.$refs.autoform.clear("title_basic",'resultUserId')
				_that.$refs.autoform.clear("title_basic",'resultInstId')
			},
			clearSupplierId(){
				let _that = this
				_that.$refs.autoform.clear("title1",'supplierId')
				_that.$refs.autoform.clear("title1",'supplierName')
				_that.$refs.autoform.clear("title1",'supplierCategoryName')
				_that.$refs.autoform.clear("title1",'dateEstablishment')
				_that.$refs.autoform.clear("title1",'unifiedSocialCreditCode')
				_that.$refs.autoform.clear("title1",'orgNatureName')
				_that.$refs.autoform.clear("title1",'supplierTypeName')
				// _that.$refs.autoform.clearGroup('title4')
				if(_that.subEditorObj["title4"]){
					let datalist = []
					_that.subEditorObj["title4"].workList = datalist
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(datalist))
					_that.$set(_that.formdata,"dataStr",JSON.stringify(datalist))
				}
				_that.$refs.autoform.clear("title5",'supplierEvaluationTypeCode')
			},
			onselectIsSupplierCode(item){
				let _that = this
				if(item.code == '38403'){
					_that.$refs.autoform.clear("title5",'effectPeriodId')
					_that.$refs.autoform.clear("title5",'effectDate')
					_that.$refs.autoform.clear("title5",'closeDate')
					_that.$refs.autoform.formdata.effectPeriodNum = ''
					_that.$refs.autoform.changeEditor("title5","effectPeriodId","type","labelbox")
					_that.$refs.autoform.changeEditor("title5","effectDate","type","labelbox")
				}else{
					_that.$refs.autoform.changeEditor("title5","effectPeriodId","type","selectbox")
					_that.$refs.autoform.changeEditor("title5","effectDate","type","newdatebox")
				}
			},
			clearIsSupplierCode(){
				let _that = this
				_that.$refs.autoform.clear("title5",'isSupplierCode')
				_that.$refs.autoform.clear("title5",'effectPeriodId')
				_that.$refs.autoform.clear("title5",'effectDate')
				_that.$refs.autoform.clear("title5",'closeDate')
				_that.$refs.autoform.formdata.effectPeriodNum = ''
			},
			onselectEffectPeriodId(item){
				let _that = this
				_that.$refs.autoform.formdata.effectPeriodNum = item.referNum
				if(_that.$refs.autoform.formdata.effectDate){
					let date = new Date(_that.$refs.autoform.formdata.effectDate);
					let closeDate = date.setDate(date.getDate() + item.referNum);
					_that.$refs.autoform.formdata.closeDate = moment(closeDate).format('YYYY-MM-DD');
				}
			},
			clearEffectPeriodId(){
				let _that = this
				_that.$refs.autoform.clear("title5",'effectPeriodId')
				_that.$refs.autoform.clear("title5",'closeDate')
				_that.$refs.autoform.formdata.effectPeriodNum = ''
			},
			onselectEffectDate(item){
				let _that = this
				let title1 = _that.$refs.autoform.getEditorTitle("title5","supplierEvaluationTypeCode")
				let title2 = _that.$refs.autoform.getEditorTitle("title5","effectDate")
				if(_that.$refs.autoform.formdata.supplierEvaluationTypeCode == '38750'){
					if(moment(item).isBefore(moment().format('YYYY-MM-DD'))){
						errorModal(_that,'提示信息',"'"+title1+"'是首次评价，'"+title2+"'需要选择当前日期或之后的日期",()=>{
							_that.$refs.autoform.clear("title5","effectDate")
						})
					}
				}
				
				
				if(_that.$refs.autoform.formdata.effectPeriodNum){
					let date = new Date(item);
					let closeDate = date.setDate(date.getDate() + _that.$refs.autoform.formdata.effectPeriodNum);
					_that.$refs.autoform.formdata.closeDate = moment(closeDate).format('YYYY-MM-DD');
				}
			},
			clearEffectDate(){
				let _that = this
				_that.$refs.autoform.clear("title5",'effectDate')
				_that.$refs.autoform.clear("title5",'closeDate')
			},
			getWorkParam(){
				let _that = this
				return {
					
				}
			},
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (let fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							_that.onSubChange(editor)
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				};
				uni.getStorage({
					key:_that.resultUserIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							
							formdata.resultUserId = d.id
							formdata.resultUserName = d.username
							_that.$refs.autoform.changeEditor('title_basic', 'resultUserId', {
								linkParam: {
									other:JSON.stringify({
										firstUserId: d.id
									}),
									url:'temployee/getStaffCombogrid',
									namefield:'userPosInstName',
									initload:true,
									searchfield:'q',
									cachename:_that.resultUserIdCacheKey
								}
							});
							_that.om.request(
								'orgInstPosUserOfficial/findInstByUserIdOrPostId',
								{
									userId: d.id,
								}
							).then(res=>{
								formdata.resultInstId = d.salaryPosInstId
								formdata.resultInstName = d.salaryPosInstName
								_that.$refs.autoform.changeEditor("title_basic","resultInstId","data",configSelectData(res,"id","instName"))
								_that.$refs.autoform.initSelectIndex("title_basic","resultInstId")
							})
							
							uni.removeStorage({
								key:_that.resultUserIdCacheKey
							})
						}
					}
				});
				uni.getStorage({
					key:_that.supplierIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.supplierId = d.id
							formdata.supplierCode = d.supplierCode
							formdata.supplierName = d.supplierName
							formdata.supplierCategoryName = d.supplierCategoryName
							formdata.dateEstablishment = d.dateEstablishment
							formdata.unifiedSocialCreditCode = d.unifiedSocialCreditCode
							formdata.orgNatureName = d.orgNatureName
							formdata.supplierTypeName = d.supplierTypeName
							
							_that.$refs.autoform.changeEditor('title1', 'supplierId', {
								linkParam: {
									other:JSON.stringify({
										oldValue: d.id
									}),
									url:'tsupplier/likeSerchAllSupplier',
									namefield:'supplierCode',
									notefield: "supplierName",
									initload:true,
									searchfield:'q',
									cachename:_that.supplierIdCacheKey
								}
							});
							
							_that.om.request(
								'tsupplierEvaluation/getItemList',
								{
									supplierId: d.id
								},
							).then(res=>{
								if(_that.subEditorObj["title4"]){
									let datalist = res.value
									_that.subEditorObj["title4"].workList = datalist
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(datalist))
									_that.$set(_that.formdata,"dataStr",JSON.stringify(datalist))
								}
							})
							
							_that.om.request(
								'tsupplierEvaluation/getSupplierEvaluationType',
								{
									supplierId: d.id
								},
							).then(res=>{
								formdata.supplierEvaluationTypeCode = res.value.value
								formdata.supplierEvaluationTypeName = res.value.name
								
								if(res.value.value == '38750'){
									if(moment(formdata.effectDate).isBefore(moment().format('YYYY-MM-DD'))){
										_that.$refs.autoform.clear("title5","effectDate")
									}
								}
							})
							
							uni.removeStorage({
								key:_that.supplierIdCacheKey
							})
						}
					}
				});
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
